package neptune.manager.installation;

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URL;

/**
 * 
 * @author Luigi Lorenzo Arpino
 * 
 */
public class Installator {
	public static final String userHome = System.getProperty("user.home");
	public static final String emuPluginDir = userHome + File.separator
			+ ".emuPlugin";
	public static final String topologyDir = emuPluginDir + File.separator
			+ "topologies";
	public static final String experimentsDir = emuPluginDir + File.separator
			+ "experiments";
	public static final String scriptsDir = emuPluginDir + File.separator
			+ "scripts";
	public static final String templatesDir = emuPluginDir + File.separator
			+ "templates";
	public static final String configurationFile = emuPluginDir
			+ File.separator + "configuration.xml";
	public static final String clusterFile = emuPluginDir + File.separator
			+ "cluster.xml";
	public static final String cloneFile = emuPluginDir + File.separator
			+ "scripts" + File.separator + "clone.sh";
	public static final String sshFile = emuPluginDir + File.separator
			+ "scripts" + File.separator + "ssh.sh";
	public static final String rolesFile = emuPluginDir + File.separator
			+ "roles.xml";
	public static final String usersFile = emuPluginDir + File.separator
			+ "users.xml";

	public Installator() {

	}

	/**
	 * Effettua l'installazione dell'applicazione
	 */
	public void install(String pathEmuPluginRootSource) {
		// Creo la root directory
		File filedir = new File(emuPluginDir);
		if (!filedir.isDirectory())
			filedir.mkdir();
		// creo cartella topologies
		filedir = new File(topologyDir);
		if (!filedir.isDirectory())
			filedir.mkdir();
		// creo cartella experiments
		filedir = new File(experimentsDir);
		if (!filedir.isDirectory())
			filedir.mkdir();
		// creo cartella script
		filedir = new File(scriptsDir);
		if (!filedir.isDirectory())
			filedir.mkdir();
		// creo la cartella templates
		filedir = new File(templatesDir);
		if (!filedir.isDirectory())
			filedir.mkdir();
		// copio il file di configurazione
		File fileDest = new File(configurationFile);
		if (!fileDest.isFile())
			copiaFile(pathEmuPluginRootSource + File.separator
					+ "configuration.xml", configurationFile);
		// copio il file di descrizione del cluster
		fileDest = new File(clusterFile);
		if (!fileDest.isFile())
			copiaFile(pathEmuPluginRootSource + File.separator + "cluster.xml",
					clusterFile);
		// copio clone.sh
		fileDest = new File(cloneFile);
		if (!fileDest.isFile())
			copiaFile(pathEmuPluginRootSource + File.separator + "scripts"
					+ File.separator + "clone.sh", cloneFile);
		fileDest.setExecutable(true);
		// copio ssh.sh
		fileDest = new File(sshFile);
		if (!fileDest.isFile())
			copiaFile(pathEmuPluginRootSource + File.separator + "scripts"
					+ File.separator + "ssh.sh", sshFile);
		fileDest.setExecutable(true);
		// copio roles.xml
		fileDest = new File(rolesFile);
		if (!fileDest.isFile())
			copiaFile(pathEmuPluginRootSource + File.separator + "roles.xml",
					rolesFile);
		// copio users.xml
		fileDest = new File(usersFile);
		if (!fileDest.isFile())
			copiaFile(pathEmuPluginRootSource + File.separator + "users.xml",
					usersFile);
		// copio tutti i templates
		filedir = new File(pathEmuPluginRootSource + File.separator
				+ "templates");
		String[] fileList = filedir.list();
		String sourcePath = pathEmuPluginRootSource + File.separator
				+ "templates" + File.separator;
		for (int i = 0; i < fileList.length; i++) {
			File file = new File(templatesDir + File.separator + fileList[i]);
			if (!file.exists())
				copiaFile(sourcePath + fileList[i], templatesDir
						+ File.separator + fileList[i]);
		}
	}

	public void copiaFile(String pathSorgente, String pathDestinazione) {
		// System.out.println("copio da "+pathSorgente+" a "+pathDestinazione);
		File fileSorgente = new File(pathSorgente);
		File fileDestinazione = new File(pathDestinazione);
		/*
		 * System.out.println("Il file esiste? " +fileSorgente.exists());
		 * System.out.println("Lunghezza del file: "+fileSorgente.length());
		 * System.out.println("E' nascosto: "+fileSorgente.isHidden());
		 */
		try {
			BufferedInputStream bis = new BufferedInputStream(
					new FileInputStream(fileSorgente));
			BufferedOutputStream bos = new BufferedOutputStream(
					new FileOutputStream(fileDestinazione));
			int i = 0;
			while ((i = bis.read()) != -1) {
				bos.write(i);
				bos.flush();
			}
			bos.close();
			bis.close();
		} catch (FileNotFoundException fnfe) {
			System.err.println(fnfe);
		} catch (IOException ioe) {
			System.err.println(ioe);
		}
	}

	public static void main(String[] args) throws MalformedURLException {
		URL url = new URL(
				"file:/home/luigi/workspace/emuPlugin/WebContent/WEB-INF/emuPlugin");
		// System.out.print(url.getPath());
		Installator inst = new Installator();
		inst.install(url.getPath());

	}
}
